<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
		<title>轨迹回放</title>
		<link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
		<style>
			html,
			body,
			#container {
				height: 100%;
				width: 100%;
			}

			.input-card .btn {
				margin-right: 1.2rem;
				width: 9rem;
			}

			.input-card .btn:last-child {
				margin-right: 0;
			}
		</style>
	</head>
	<body>
		<!--uni SDK 一定要引入 -->
		<script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js">
		</script>
		<script type="text/javascript"
			src="https://webapi.amap.com/maps?v=1.4.15&key=694b5c0a3cba26f47bbe3ca7fd7c8908&plugin=AMap.GraspRoad">
		</script>
		<div id="container"></div>
		<div class="input-card">
			<h4>轨迹回放控制</h4>
			<div class="input-item">
				<input type="button" class="btn" value="开始回放" id="start" onclick="startAnimation()" />
				<input type="button" class="btn" value="暂停回放" id="pause" onclick="pauseAnimation()" />
			</div>
			<div class="input-item">
				<input type="button" class="btn" value="继续回放" id="resume" onclick="resumeAnimation()" />
				<input type="button" class="btn" value="停止回放" id="stop" onclick="stopAnimation()" />
			</div>
		</div>
		<script type="text/javascript">
			var marker, lineArr = [];
			const graspRoad = new AMap.GraspRoad();
			var map = new AMap.Map("container", {
				resizeEnable: true,
				center: [114.205173, 30.63413],
				zoom: 17
			});

			window.receiveData = function(data) {
				// graspRoad.driving(data, function(error, result) {
				// 	console.log(error)
				// 	console.log(result)
				// 	if (!error) {
				// 		// var path2 = [];
				// 		// var newPath = result.data.points;
				// 		// for (var i = 0; i < newPath.length; i += 1) {
				// 		// 	path2.push([newPath[i].x, newPath[i].y])
				// 		// }
				// 		// var newLine = new AMap.Polyline({
				// 		// 	path: path2,
				// 		// 	strokeWeight: 8,
				// 		// 	strokeOpacity: 0.8,
				// 		// 	strokeColor: '#0091ea',
				// 		// 	showDir: true
				// 		// })
				// 		// map.add(newLine)
				// 		// map.setFitView()
				// 	}
				// })
				lineArr = JSON.parse(data)

				marker = new AMap.Marker({
					map: map,
					position: lineArr[0],
					icon: "../../static/img/bluecar.png",
					offset: new AMap.Pixel(-26, -13),
					autoRotation: true,
					angle: -90,
				});
				// 绘制轨迹
				var polyline = new AMap.Polyline({
					map: map,
					path: lineArr,
					showDir: true,
					strokeColor: "#28F", //线颜色
					// strokeOpacity: 1,     //线透明度
					strokeWeight: 6, //线宽
					// strokeStyle: "solid"  //线样式
				});

				var passedPolyline = new AMap.Polyline({
					map: map,
					// path: lineArr,
					strokeColor: "#AF5", //线颜色
					// strokeOpacity: 1,     //线透明度
					strokeWeight: 6, //线宽
					// strokeStyle: "solid"  //线样式
				});

				marker.on('moving', function(e) {
					passedPolyline.setPath(e.passedPath);
				});

				map.setFitView();
			}

			function startAnimation() {
				marker.moveAlong(lineArr, 1000);
			}

			function pauseAnimation() {
				marker.pauseMove();
			}

			function resumeAnimation() {
				marker.resumeMove();
			}

			function stopAnimation() {
				marker.stopMove();
			}
		</script>
	</body>
</html>
